package com.cskaoyan.gateway.controller.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.gateway.form.user.UserForm;
import com.mall.user.dto.UserGetLoginRequest;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.IKaptchaService;

import com.mall.user.IUserLoginService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.*;
import com.mall.user.intercepter.TokenIntercepter;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.mall.user.intercepter.TokenIntercepter.USER_INFO_KEY;

/**
 * @program: project3-group12
 * @description:
 * @author: YuHui
 * @create: 2021-08-25 17:11
 **/
@RestController
@RequestMapping("user")
public class LoginController {

    @Reference(timeout = 20000, check = false)
    private IKaptchaService iKaptchaService;

    @Reference(timeout = 20000, check = false)
    private IUserLoginService iUserLoginService;


    /**
     * 验证登录
     *
     * @param request
     * @return
     */
    @GetMapping("login")
    public ResponseData loginGet(HttpServletRequest request) {

        //把我们获取的token信息解析出来
//        CheckAuthRequest checkAuthRequest = new CheckAuthRequest();
//        String access_token = CookieUtil.getCookieValue(request, TokenIntercepter.ACCESS_TOKEN);
//
//        checkAuthRequest.setToken(access_token);
//        CheckAuthResponse checkAuthResponse = iUserLoginService.validToken(checkAuthRequest);
//        if (!checkAuthResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
//            return new ResponseUtil<>().setErrorMsg(checkAuthResponse.getMsg());
//        }
//
//        //我们用户的token，就是我们的用户名
//        String userinfo = checkAuthResponse.getUserinfo();
//        UserGetLoginRequest userGetLoginRequest = new UserGetLoginRequest();
//        userGetLoginRequest.setUserinfo(userinfo);

        String userinfo = (String) request.getAttribute(USER_INFO_KEY);

        UserGetLoginRequest userGetLoginRequest = new UserGetLoginRequest();
        userGetLoginRequest.setUserinfo(userinfo);

        //查询对应的信息
        UserGetLoginResponse userGetLoginResponse = iUserLoginService.checklogin(userGetLoginRequest);
        if (!userGetLoginResponse.getCode().equals((SysRetCodeConstants.SUCCESS.getCode()))) {
            return new ResponseUtil<>().setErrorMsg(userGetLoginResponse.getMsg());
        }
        return new ResponseUtil().setData(userGetLoginResponse);
    }

    /**
     * 登陆
     *
     * @param userForm
     * @param response
     * @return
     */
    @Anoymous
    @PostMapping("login")
    public ResponseData login(@RequestBody UserForm userForm, HttpServletRequest request, HttpServletResponse response) {

        //验证码
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        String uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        kaptchaCodeRequest.setCode(userForm.getCaptcha());
        kaptchaCodeRequest.setUuid(uuid);
        KaptchaCodeResponse kaptchaCoderesponse =
                iKaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        if (!SysRetCodeConstants.SUCCESS.getCode().equals(kaptchaCoderesponse.getCode())) {
            return new ResponseUtil<>().setErrorMsg(kaptchaCoderesponse.getMsg());
        }

        UserLoginRequest userLoginRequest = new UserLoginRequest();
        userLoginRequest.setUserName(userForm.getUserName());
        userLoginRequest.setPassword(userForm.getUserPwd());
        UserLoginResponse userLoginResponse = iUserLoginService.Login(userLoginRequest);

        if (SysRetCodeConstants.SUCCESS.getCode().equals(userLoginResponse.getCode())) {
            //说明登陆成功
            Cookie cookie = CookieUtil.genCookie(TokenIntercepter.ACCESS_TOKEN, userLoginResponse.getToken(), "/", 60 * 60 * 24);
            cookie.setHttpOnly(true);
            response.addCookie(cookie);
            return new ResponseUtil().setData(userLoginResponse);
        }

        //登陆失败
        return new ResponseUtil().setErrorMsg(userLoginResponse.getMsg());
    }

}
